/**
 * 查询区划
 */
export function search() {

}

export function getFeatureInLayer(region, layer) {
  let fs = layer.features;
  for(let i=fs.length; i--; ) {
    if(fs[i].attributes.id === region.id) {
      return fs[i];
    }
  }
  return false;
}

export function clearSelects(selectedFeatures, layer_region) {
  let len = selectedFeatures.length;
	if(len === 0) {
		return;
	}
	for(var i=len; i--; ) {
		var sf = selectedFeatures[i];
		var f = getFeatureInLayer(sf.attributes, layer_region );
		if(f) {
			f.style = f.attributes.oldStyle;
		}		
	}
	layer_region.redraw();
	selectedFeatures = [];
}

/**
 * 选择区划
 */
export function select(feature, layer_region, selectedFeatures) {
  var theEvent = window.event;

  //按住ctrl可多选	
  // if ((isFirefox && !isCtrlKeydown) || (!isFirefox && !theEvent.ctrlKey)) {
  if (!theEvent.ctrlKey) {
    clearSelects(selectedFeatures, layer_region);
  }

  feature.attributes.oldStyle = feature.style;
  feature.style = getSelectStyle();
  var index = selectedFeatures.indexOf(feature);//重复选择判断				
  if (index < 0) {
    //不重复添加
    selectedFeatures.push(feature);
  }

  /*Dituhui.Zone.Table.scrollToFeature(feature);

  if (typeof (openInfoWindow) === "undefined" || openInfoWindow == true) {
    Map.openRegionAttrPopup(feature);
  }

  if (layer_region.visibility == false) {
    var record = selectedFeature.attributes;
    var geoText = new SuperMap.Geometry.GeoText(record.center.x, record.center.y, record.name);
    var geotextFeature = new SuperMap.Feature.Vector(geoText, record);
    layer_region_temp.addFeatures([selectedFeature.clone()]);
    layer_region_label_temp.addFeatures([geotextFeature]);
  }*/

  layer_region.redraw();
}

/**
 * 将区划显示在地图上
 * @param data 需要显示的区划数据
 */
export function showRegionsToMap(data, geolayer, textlayer, map) {
  geolayer.removeAllFeatures();
  textlayer.removeAllFeatures();
  var len = data.length;
  if (len === 0) {
    return;
  }
  var fs = [], ls = [];
  for (var i = len; i--;) {
    var record = data[i];
    if (!record.name) {
      record.name = "";
    }
    var point2Ds = record.points;
    var parts = record.parts;
    if (!parts || parts.length == 0) {
      continue;
    }
    var attr = record;
    attr.parts = null;
    attr.points = null;
    attr.type = "region";
    attr.branch_name = getBranchName(record.pointnames);

    var geometry = drawRegion(parts, point2Ds);
    var style = getStyle(record.name);
    attr.oldStyle = style;
    var feature = new SuperMap.Feature.Vector(geometry, attr, style);
    fs.push(feature);

    var geoText = new SuperMap.Geometry.GeoText(record.center.x, record.center.y, record.name);

    var geotextFeature = new SuperMap.Feature.Vector(geoText);
    geotextFeature.style = getTextStyle(record.area_status);

    var attr_label = attr;
    attr_label.type = "text-region";
    geotextFeature.attributes = attr_label;
    ls.push(geotextFeature);
  }
  geolayer.addFeatures(fs);
  textlayer.addFeatures(ls);
  map.zoomToExtent(geolayer.getDataExtent());
}

/** 
 * 边界样式
 */
export function getBoundryStyle() {
  return {
    fill: false,
    strokeColor: "#ff6666",
    strokeWidth: 3
  };
}

/**
 * 普通区划样式
 */
export function getStyle() {
  let style = {
    fill: true,
    fillColor: "#b0fff3",
    fillOpacity: 0.2,
    strokeColor: "#0090ff",
    strokeOpacity: 0.8,
    strokeWidth: 2,
    fontColor: "#006cff",
    fontOpacity: "1",
    fontFamily: "microsoft yahei",
    fontSize: "14px",
    fontWeight: "bold",
    fontStyle: "normal",
    labelSelect: "true",
    cursor: "pointer"
  }
  return style;
}

/** 
 * 区划文字样式
 */
export function getTextStyle(status) {
  var fillcolor = "#006cff";
  if (typeof (status) !== "undefined" && status == 1) {
    fillcolor = "#ff0000";
  }
  let style = {
    fontColor: "#fff",
    fontWeight: "normal",
    fontSize: "14px",
    fontFamily: "microsoft yahei",
    fill: true,
    fillColor: fillcolor,
    fillOpacity: 1,
    stroke: true,
    strokeColor: "#8B7B8B"
  }
  return style;
}
/**
 * 停用的区划文字显示红底白字
 */
export function getTextStyleGroups() {
  return [
    {
      start: 0,
      end: 0.5,
      style: getTextStyle()
    },
    {
      start: 0.5,
      end: 2,
      style: getTextStyle(1)
    },
  ]
}


/** 
 * 区划选中样式
 */
export function getSelectStyle() {
  var style = {
    fill: true,
    fillColor: "#ff4568",
    fillOpacity: 0.4,
    strokeColor: "#0090ff",
    strokeOpacity: 0.8,
    strokeWidth: 2,
    fontColor: "#006cff",
    fontOpacity: "1",
    cursor: "pointer"
  }
  return style;
}

/** 
 * 关联区划选中样式
 */
export function getRelatedSelectStyle() {
  var style = {
    fill: true,
    fillColor: "#b0fff3",
    fillOpacity: 0.6,
    strokeColor: "#0090ff",
    strokeOpacity: 1,
    strokeWidth: 3,
    labelSelect: "true",
    cursor: "pointer"
  }
  return style;
}

/** 
 * 线拆分样式
 */
export function getSplitLineStyle() {
  var style = {
    fillColor: "blue",
    strokeColor: "blue",
    pointRadius: 4,
    strokeWidth: 2,
    strokeDashstyle: "dash"
  }
  return style;
}


/** 
 * 面拆分样式
 */
export function getSplitAreaStyle() {
  var style = {
    fillColor: "white",
    fillOpacity: 0.2,
    strokeColor: "blue",
    pointRadius: 4,
    strokeWidth: 2,
    strokeDashstyle: "dash"
  }
  return style;
}

/** 
 * 沿路画区-打点样式
 */
export function getRouteRegionPointStyle(label) {
  var style = {
    strokeColor: "#0090ff",
    strokeOpacity: 1,
    strokeWidth: 1,
    pointRadius: 10,
    graphicName: "circle",
    fillColor: '#FFFFFF',
    fillOpacity: 1,
    cursor: 'pointer',
    graphicTitle: label,
    label: label
  }
  return style;
}
/** 
 * 区划捕捉 点样式
 */
export function getSnapPointStyle() {
  var style = {
    strokeColor: "#0090ff",
    strokeOpacity: 1,
    strokeWidth: 1,
    pointRadius: 5,
    graphicName: "circle",
    fillColor: '#0090ff',
    fillOpacity: 1,
    cursor: 'pointer'
  }
  return style;
}
/** 
 * 沿路画区-线路样式
 */
export function getPathRegionLineStyle() {
  var style = {
    fillColor: "blue",
    strokeColor: "blue",
    pointRadius: 4,
    strokeWidth: 4,
    strokeDashstyle: "solid",
    strokeOpacity: 0.6
  }
  return style;
}


/** 
 * 区划修改样式
 */
export function getRegionEditStyle() {
  var style = {
    cursor: "inherit",
    fillColor: "blue",
    fillOpacity: 0.3,
    fontColor: "#000000",
    hoverFillColor: "white",
    hoverFillOpacity: 0.3,
    hoverPointRadius: 1,
    hoverPointUnit: "%",
    hoverStrokeColor: "red",
    hoverStrokeOpacity: 1,
    hoverStrokeWidth: 0.2,
    labelAlign: "cm",
    labelOutlineColor: "white",
    labelOutlineWidth: 3,
    pointRadius: 6,
    pointerEvents: "visiblePainted",
    strokeColor: "blue",
    strokeDashstyle: "solid",
    strokeLinecap: "round",
    strokeOpacity: 1,
    strokeWidth: 1
  }
  return style;
}


/** 
 * 解析区域面数据，生成geometry
 */
export function drawRegion(parts, point2Ds) {
  var length_parts = (parts.length == 0 ? 1 : parts.length);
  var length_point2Ds = point2Ds.length;

  //取点的索引
  var idxPoint = 0;
  var count = 0;

  var regions = [];

  for (var k = 0; k < length_parts; k++) {

    //每个小多边形的点数
    var pntCount = parts[k];

    //得到区片
    var points = [];
    var index = -1;
    for (var j = idxPoint; j < idxPoint + pntCount; j++) {
      var item = point2Ds[j];
      var pp = new SuperMap.Geometry.Point(Number(item.x), Number(item.y));
      /*for (var l=0; l < points.length; l++) {
          if (j == idxPoint + pntCount - 1 
              && l != 0 
              && points[l].x == pp.x && points[l].y == pp.y){
              if (points[l].x != points[0].x && points[l].y != points[0].y) {
                  index=l;
              }
          }
      }*/
      points.push(pp);
    }

    if (index != -1) {
      //          regions.push( new SuperMap.Geometry.Polygon( new SuperMap.Geometry.LinearRing( points.splice(index) ) ) );
      regions.push(new SuperMap.Geometry.LinearRing(points.splice(index)));
    }
    idxPoint = idxPoint + pntCount;

    /*if(k !== 241 && k != 472) {
    continue;
  }
  if(k==241) {
    console.log("parts: 241")
    console.dir(points);
  }
  if(k==472) {
    console.log("parts: 241");
    console.dir(points);
  }*/

    //      regions.push( new SuperMap.Geometry.Polygon([new SuperMap.Geometry.LinearRing( points )]));
    regions.push(new SuperMap.Geometry.LinearRing(points));
  }
  //  var georegion = new SuperMap.Geometry.MultiPolygon(regions);    
  var georegion = new SuperMap.Geometry.Polygon(regions);
  return georegion;
}


/**
 * 转换区划绑定的网点
 */
export function getBranchName(pointnames) {
  var len = pointnames ? pointnames.length : 0;
  var branch_name = "";
  if (len > 0) {
    for (var k = 0; k < len; k++) {
      branch_name += pointnames[k];
      if (k < (len - 1)) {
        branch_name += ",";
      }
    }
  }
  return branch_name;
}